﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.VisualBasic;
using Newtonsoft.Json;
using Rubyer;
using Rubyer.Commons;
using StockUI.Entity;
using StockUI.Model;
using StockUI.Views;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media.Media3D;
using System.Windows.Threading;

namespace StockUI.ViewModels
{
   public partial class NoteTaskViewModel :ObservableObject
    {
        /// <summary>
        /// 树形菜单
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<TreeDataModel> noteTasks;
        

        /// <summary>
        /// 当前内容
        /// </summary>
        [ObservableProperty]
        private NoteSelectItem notetaskSelectItem;


        /// <summary>
        /// 选中项目
        /// </summary>
        [ObservableProperty]
        private TreeDataModel selectedItem;
        public NoteTaskViewModel()
        {
            NotetaskSelectItem = new NoteSelectItem(0,"请输入主题","请输入内容");
           
            ReloadData();

            
        }

        private void ReloadData()
        {
           
            NoteTasks = new ObservableCollection<TreeDataModel> ();
            DateTime dt = DateTime.Now;
            var notetasklist = new SQLiteHelper<NoteTaskDB>().Query(a => a.Year == dt.Year);
            ObservableCollection<TreeDataModel> trmonth = new ObservableCollection<TreeDataModel>();
            for (int i = 1; i < 13; i++)
            {
                TreeDataModel tmonth = new TreeDataModel();
                tmonth.Name = $"{dt.Year}年{i}月";
                tmonth.Code = $"{i}月";
                var notes = notetasklist.Where(a => a.Month == i).Select(a => new TreeDataModel { Code = a.Id.ToString(), Name = a.Title.StringSub(8)}).ToList();
                ObservableCollection<TreeDataModel> childrens = new ObservableCollection<TreeDataModel>();
                foreach (var item in notes)
                {
                    childrens.Add(item);
                }
                tmonth.Children = childrens;
                trmonth.Add(tmonth);
            }
            // treeData.Children = trmonth;
            NoteTasks = trmonth;
        }

       
        /// <summary>
        /// 下一步命令
        /// </summary>
        [RelayCommand]
        private async Task NoteContent(TreeDataModel treedata)
        {
            var id = int.Parse(treedata.Code);
            var single = new SQLiteHelper<NoteTaskDB>().QuerySingle(a => a.Id == id);
            if (single != null)
            {
                NotetaskSelectItem = new NoteSelectItem(single.Id, single.Title, single.Content);
            }
            else
            {
                NotetaskSelectItem = new NoteSelectItem(0, "请输入主题", "请输入内容");
            }
            
        }
        /// <summary>
        /// 新建命令
        /// </summary>
        [RelayCommand]
        private async Task New()
        {
            NotetaskSelectItem = new NoteSelectItem(0, "请输入主题", "请输入内容");
            SelectedItem = new TreeDataModel { Code = "0", Name = DateTime.Now.ToString("yyyy年MM月") };
        }
        /// <summary>
        /// 保存命令
        /// </summary>
        [RelayCommand]
        private async Task Add()
        {
            if (NotetaskSelectItem != null)
            {
                var m = new NoteTaskDB
                {
                    Content = NotetaskSelectItem.Content,
                    Title = NotetaskSelectItem.Title,
                    CreateDate = DateTime.Now,
                    Year = DateTime.Now.Year,
                    Month = DateTime.Now.Month,
                };
                if (NotetaskSelectItem.Id > 0)
                {
                    m.Id = NotetaskSelectItem.Id;
                    new SQLiteHelper<NoteTaskDB>().Update(m);
                    await MessageBoxR.Show("修改成功");
                  
                    foreach (var note in NoteTasks)
                    {
                        if (note.Children is { })
                        {
                            for (int i = 0; i < note.Children.Count; i++)
                            {
                                var id = note.Children[i].Code;
                                if (id == NotetaskSelectItem.Id.ToString())
                                {
                                    note.Children[i].Name = NotetaskSelectItem.Title.StringSub(8);
                                    return;
                                }
                            }
                        }
                    }
                }
                else
                {
                    new SQLiteHelper<NoteTaskDB>().Insert(m);
                    var addnote = NoteTasks.Where(a => a.Code == $"{DateTime.Now.Month}月").First();
                    addnote.Children ??= new ObservableCollection<TreeDataModel>();
                    addnote.Children.Add(new TreeDataModel() { Code = m.Id.ToString(), Name = NotetaskSelectItem.Title, IsSelected = true });
                    await MessageBoxR.Show("保存成功");
                }  
            }
          
        }

        /// <summary>
        /// 删除命令
        /// </summary>
        [RelayCommand]
        private async Task Delete()
        {
            if (NotetaskSelectItem != null && NotetaskSelectItem.Id > 0)
            {
                var result = await MessageBoxR.Confirm("是否删除信息？");
                if (result == MessageBoxResult.Yes)
                {
                    new SQLiteHelper<NoteTaskDB>().Delete(NotetaskSelectItem.Id);
                    await MessageBoxR.Show("删除成功");

                    foreach (var note in NoteTasks)
                    {
                        if (note.Children is { })
                        {
                            for (int i = 0; i < note.Children.Count; i++)
                            {
                                var id = note.Children[i].Code;
                                if (id == NotetaskSelectItem.Id.ToString())
                                {
                                    note.Children.Remove(note.Children[i]);
                                    NotetaskSelectItem = new NoteSelectItem(0, "请输入主题", "请输入内容");
                                    SelectedItem = new TreeDataModel { Code = "0", Name = DateTime.Now.ToString("yyyy年MM月") };
                                    return;
                                }
                            }
                        }
                    }

                }
            }
          
        }
    }
}
